//
//  SAPrintSkuColorSizeLine.m
//  Saas
//
//  Created by 杨荣 on 2020/6/8.
//  Copyright © 2020 bing. All rights reserved.
//

#import "SAPrintSkuColorSizeLine.h"
#import "SASalesInvoiceDetailModel.h"
#import "SASalesOrderViewListModel.h"
#import "NSString+Print.h"

@interface SAPrintSkuColorSizeLine ()

@property (nonatomic, strong) ESCPrinter *escPrinter;

@end

@implementation SAPrintSkuColorSizeLine

- (NSArray <NSString *>*)printSkuColorSizeLineWithProductSkuArray:(NSArray <SAProductListModel *>*)productSkuArray printerByteWidth:(NSInteger)printerByteWidth {
    
    NSMutableArray <NSString *>*printStrArray = [NSMutableArray array];// 打印字符串集合
    
    NSArray *originXArray = @[@(0),@(0.28*printerByteWidth),@(0.42*printerByteWidth),@(0.61*printerByteWidth),@(0.78*printerByteWidth)];
    NSString *partingLine = [NSString getPrintPartingLineWithByteWidth:printerByteWidth originX:0];
    
    // 分割线
    [printStrArray addObject:partingLine];
    
    // 标题栏
    NSString *titleStr = [NSString getPrintTextWithTextInfoArray:@[
                          @{@"商品":originXArray[0]},
                          @{@"数量":originXArray[1]},
                          @{@"单价":originXArray[2]},
                          @{@"折扣":originXArray[3]},
                          @{@"折后金额":originXArray[4]},
    ] font:(NSStringFontSmall)];
    [printStrArray addObject:titleStr];
    
    // 分割线
    [printStrArray addObject:partingLine];
    
    // 商品sku
    NSArray <SAProductListModel *>*procutArray = [productSkuArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        SAProductListModel *model = evaluatedObject;
        return model.productCode.length > 0;
    }]];
    
    for (SAProductListModel *product in procutArray) {
        NSString *title = [NSString stringWithFormat:@"%@,%@",product.productCode,product.productName];
        [printStrArray addObject:title];
        for (SASaleSkuListModel *sku in product.skuList) {
            NSString *colourSize = [NSString stringWithFormat:@"%@/%@",sku.colourName,sku.sizeName];
            
            NSString *count = [self getTruncation:sku.count And:6];
            NSString *price = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.price doubleValue]/100.f] And:8];
            NSString *discountRate = [self getTruncation:[NSString stringWithFormat:@"%0.2f%%",[sku.discountRate doubleValue]/100.f] And:8];
            NSString *discountPrice = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.discountPrice doubleValue]/100.f] And:10];
            
            NSString *str = [NSString getPrintTextWithTextInfoArray:@[
                                  @{colourSize:originXArray[0]},
                                  @{count:originXArray[1]},
                                  @{price:originXArray[2]},
                                  @{discountRate:originXArray[3]},
                                  @{discountPrice:originXArray[4]},
            ] font:(NSStringFontSmall)];
            title = @"";
            [printStrArray addObject:str];
        }
        // 分割线
        [printStrArray addObject:partingLine];
    }
    return printStrArray;
}

- (NSArray <NSString *>*)warehousingPrintSkuColorSizeLineWithProductSkuArray:(NSArray <SASalesOrderViewListModel *>*)productSkuArray printerByteWidth:(NSInteger)printerByteWidth {
    NSMutableArray <NSString *>*printStrArray = [NSMutableArray array];// 打印字符串集合
    
    NSArray *originXArray = @[@(0),@(0.34*printerByteWidth),@(0.56*printerByteWidth),@(0.78*printerByteWidth)];
    NSString *partingLine = [NSString getPrintPartingLineWithByteWidth:printerByteWidth originX:0];
    
    // 分割线
    [printStrArray addObject:partingLine];
    
    // 标题栏
    NSString *titleStr = [NSString getPrintTextWithTextInfoArray:@[
                          @{@"商品":originXArray[0]},
                          @{@"数量":originXArray[1]},
                          @{@"单价":originXArray[2]},
                          @{@"小计":originXArray[3]},
    ] font:(NSStringFontSmall)];
    [printStrArray addObject:titleStr];
    
    // 分割线
    [printStrArray addObject:partingLine];
    
    // 商品sku
    NSArray <SASalesOrderViewListModel *>*procutArray = [productSkuArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        SASalesOrderViewListModel *model = evaluatedObject;
        return model.productCode.length > 0;
    }]];
    for (SASalesOrderViewListModel *product in procutArray) {
        NSString *title = [NSString stringWithFormat:@"%@,%@",product.productCode,product.productName];
//        title = [title getSubStringWithByteWidthMax:[titleOriginXArray[0] integerValue] font:(NSStringFontSmall)];
//        NSString *titleStr = [NSString getPrintTextWithTextInfoArray:@[
//                              @{title:titleOriginXArray[0]},
//        ] font:(NSStringFontSmall)];
        [printStrArray addObject:title];
        for (SASaleSkuListModel *sku in product.toPendSkuAttrs) {
            NSString *colourSize = [NSString stringWithFormat:@"%@/%@",sku.colourName,sku.sizeName];
            
            NSString *count = [self getTruncation:sku.count And:6];
            NSString *price = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.price doubleValue]/100.f] And:10];
            NSString *discountPrice = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.price doubleValue]/100.f * [sku.count doubleValue]] And:10];
           
            NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
            if ([productViewCostPrice isEqualToString:@"1"]) {
                price = @"******";
                discountPrice = @"******";
            }
            
            NSString *str = [NSString getPrintTextWithTextInfoArray:@[
                                  @{colourSize:originXArray[0]},
                                  @{count:originXArray[1]},
                                  @{price:originXArray[2]},
                                  @{discountPrice:originXArray[3]},
            ] font:(NSStringFontSmall)];
            title = @"";
            [printStrArray addObject:str];
        }
        // 分割线
        [printStrArray addObject:partingLine];
    }
    return printStrArray;
}

- (NSArray <NSData *>*)warehousingPrintSkuColorSizeLineWithProductSkuArray2:(NSArray <SASalesOrderViewListModel *>*)productSkuArray printerByteWidth:(NSInteger)printerByteWidth {
    NSMutableArray <NSData *>*printStrArray = [NSMutableArray array];// 打印字符串集合
    
    NSArray *originXArray = @[@(0),@(0.34*printerByteWidth),@(0.49*printerByteWidth),@(0.73*printerByteWidth)];
    NSArray *originXArray2 = @[@(0),@(0.05*printerByteWidth),@(0.23*printerByteWidth),@(0.49*printerByteWidth)];
    NSString *partingLine = [NSString getPrintPartingLineWithByteWidth:printerByteWidth originX:0];
    
    // 分割线
    NSData *data = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data];
    
    // 标题栏
    NSString *titleStr = [NSString getPrintTextWithTextInfoArray:@[
                          @{@"商品":originXArray[0]},
                          @{@"数量":originXArray[1]},
                          @{@"单价":originXArray[2]},
                          @{@"小计":originXArray[3]},
    ] font:(NSStringFontSmall)];
    NSData *data2 = [self.escPrinter printSamallText:titleStr font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data2];
    
    // 分割线
    NSData *data3 = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data3];
    
    // 商品sku
    NSArray <SASalesOrderViewListModel *>*procutArray = [productSkuArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        SASalesOrderViewListModel *model = evaluatedObject;
        return model.productCode.length > 0;
    }]];
    for (SASalesOrderViewListModel *product in procutArray) {
        NSString *title = [NSString stringWithFormat:@"%@,%@",product.productCode,product.productName];
//        title = [title getSubStringWithByteWidthMax:[titleOriginXArray[0] integerValue] font:(NSStringFontSmall)];
//        NSString *titleStr = [NSString getPrintTextWithTextInfoArray:@[
//                              @{title:titleOriginXArray[0]},
//        ] font:(NSStringFontSmall)];
        NSData *data4 = [self.escPrinter printText:title font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
        [printStrArray addObject:data4];

        for (SASaleSkuListModel *sku in product.toPendSkuAttrs) {
            NSString *colourSize = [NSString stringWithFormat:@"%@/%@                 ",sku.colourName,sku.sizeName];//加空格是为了避免字符截取的时候过短
            NSString *newColourSize = [colourSize getSubStringWithByteWidthMax:12 font:NSStringFontSmall];
            
            NSString *count = [self getTruncation:sku.count And:6];
            NSString *price = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.price doubleValue]/100.f] And:10];
            NSString *discountPrice = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.price doubleValue]/100.f * [sku.count doubleValue]] And:10];
            
            NSString *productViewCostPrice = [NSString stringWithFormat:@"%@",[SAValueManger shareInStance].userInfo.menuList[1][@"perms"][@"productViewCostPrice"]];
            if ([productViewCostPrice isEqualToString:@"1"]) {
                price = @"******";
                discountPrice = @"******";
            }
            
//            NSString *str = [NSString getPrintTextWithTextInfoArray:@[
//                @{colourSize:originXArray[0]},@{@"":originXArray2[1]},@{@"":originXArray2[2]},@{@"":originXArray2[3]}] font:(NSStringFontSmall)];
            
            NSString *str2 = [NSString getPrintTextWithTextInfoArray:@[
                                  @{@"":originXArray2[0]},
                                  @{count:originXArray2[1]},
                                  @{price:originXArray2[2]},
                                  @{discountPrice:originXArray2[3]},
            ] font:(NSStringFontSmall)];
            
            title = @"";
            
            NSData *data5 = [self.escPrinter noLineprintText:newColourSize font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
            [printStrArray addObject:data5];
            
            NSData *data6 = [self.escPrinter printSamallText:str2 font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
            [printStrArray addObject:data6];
        }
        // 分割线
        NSData *data7 = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
        [printStrArray addObject:data7];
    }
    return printStrArray;
}

- (NSArray <NSData *>*)printSkuColorSizeLineWithProductSkuArray2:(NSArray <SAProductListModel *>*)productSkuArray printerByteWidth:(NSInteger)printerByteWidth {
    
    NSMutableArray <NSData *>*printStrArray = [NSMutableArray array];// 打印字符串集合
    
    NSArray *originXArray = @[@(0),@(0.24*printerByteWidth),@(0.37*printerByteWidth),@(0.50*printerByteWidth),@(0.64*printerByteWidth)];
    NSArray *originXArray2 = @[@(0),@(0.03*printerByteWidth),@(0.16*printerByteWidth),@(0.35*printerByteWidth),@(0.50*printerByteWidth)];
    NSString *partingLine = [NSString getPrintPartingLineWithByteWidth:printerByteWidth originX:0];
    
    // 分割线
    NSData *data = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data];
    
    // 标题栏
    NSString *titleStr = [NSString getPrintTextWithTextInfoArray:@[
                          @{@"商品":originXArray[0]},
                          @{@"数量":originXArray[1]},
                          @{@"单价":originXArray[2]},
                          @{@"折扣":originXArray[3]},
                          @{@"折后金额":originXArray[4]},
    ] font:(NSStringFontSmall)];
    NSData *data2 = [self.escPrinter printSamallText:titleStr font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data2];
    
    // 分割线
    NSData *data3 = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data3];
    
    // 商品sku
    NSArray <SAProductListModel *>*procutArray = [productSkuArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        SAProductListModel *model = evaluatedObject;
        return model.productCode.length > 0;
    }]];
    
    for (SAProductListModel *product in procutArray) {
        NSString *title = [NSString stringWithFormat:@"%@,%@",product.productCode,product.productName];
        NSData *data4 = [self.escPrinter printText:title font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
        [printStrArray addObject:data4];
        for (SASaleSkuListModel *sku in product.skuList) {
            NSString *colourSize = [NSString stringWithFormat:@"%@/%@          ",sku.colourName,sku.sizeName];
            NSString *newColourSize = [colourSize getSubStringWithByteWidthMax:10 font:NSStringFontSmall];
            
            NSString *count = [self getTruncation:sku.count And:6];
            NSString *price = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.price doubleValue]/100.f] And:7];
            NSString *discountRate = [self getTruncation:[NSString stringWithFormat:@"%0.2f%%",[sku.discountRate doubleValue]/100.f] And:7];
            NSString *discountPrice = [self getTruncation:[NSString stringWithFormat:@" %0.2f",[sku.discountPrice doubleValue]/100.f] And:10];
            
            NSString *str = [NSString getPrintTextWithTextInfoArray:@[
                                  @{@"":originXArray2[0]},
                                  @{count:originXArray2[1]},
                                  @{price:originXArray2[2]},
                                  @{discountRate:originXArray2[3]},
                                  @{discountPrice:originXArray2[4]},
            ] font:(NSStringFontSmall)];
            title = @"";
            NSData *data5 = [self.escPrinter noLineprintText:newColourSize font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
            [printStrArray addObject:data5];
            
            NSData *data6 = [self.escPrinter printSamallText:str font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
            [printStrArray addObject:data6];
        }
        // 分割线
        NSData *data7 = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
        [printStrArray addObject:data7];
    }
    return printStrArray;
}

- (NSArray <NSString *>*)printSkuColorSizeLineWithProductSkuArray3:(NSArray <SAProductListModel *>*)productSkuArray printerByteWidth:(NSInteger)printerByteWidth {
    NSMutableArray <NSString *>*printStrArray = [NSMutableArray array];// 打印字符串集合
    
    NSArray *originXArray = @[@(0),@(0.28*printerByteWidth),@(0.5*printerByteWidth),@(0.7*printerByteWidth)];
    NSString *partingLine = [NSString getPrintPartingLineWithByteWidth:printerByteWidth originX:0];
    
    // 分割线
    [printStrArray addObject:partingLine];
    
    // 标题栏
    NSString *titleStr = [NSString getPrintTextWithTextInfoArray:@[
                          @{@"商品":originXArray[0]},
                          @{@"数量":originXArray[1]},
                          @{@"单价":originXArray[2]},
                          @{@"金额":originXArray[3]},
    ] font:(NSStringFontSmall)];
    [printStrArray addObject:titleStr];
    
    // 分割线
    [printStrArray addObject:partingLine];
    
    // 商品sku
    NSArray <SAProductListModel *>*procutArray = [productSkuArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        SAProductListModel *model = evaluatedObject;
        return model.productCode.length > 0;
    }]];
    
    for (SAProductListModel *product in procutArray) {
        NSString *title = [NSString stringWithFormat:@"%@,%@",product.productCode,product.productName];
        [printStrArray addObject:title];
        for (SASaleSkuListModel *sku in product.skuList) {
            NSString *colourSize = [NSString stringWithFormat:@"%@/%@",sku.colourName,sku.sizeName];
            
            NSString *count = [self getTruncation:sku.count And:6];
            NSString *price = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.price doubleValue]/100.f] And:8];
            NSString *discountPrice = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.discountPrice doubleValue]/100.f] And:10];
            
            NSString *str = [NSString getPrintTextWithTextInfoArray:@[
                                  @{colourSize:originXArray[0]},
                                  @{count:originXArray[1]},
                                  @{price:originXArray[2]},
                                  @{discountPrice:originXArray[3]},
            ] font:(NSStringFontSmall)];
            title = @"";
            [printStrArray addObject:str];
        }
        // 分割线
        [printStrArray addObject:partingLine];
    }
    return printStrArray;
}

- (NSArray <NSData *>*)printSkuColorSizeLineWithProductSkuArray4:(NSArray <SAProductListModel *>*)productSkuArray printerByteWidth:(NSInteger)printerByteWidth {
    NSMutableArray <NSData *>*printStrArray = [NSMutableArray array];// 打印字符串集合
    
    NSArray *originXArray = @[@(0),@(0.24*printerByteWidth),@(0.46*printerByteWidth),@(0.65*printerByteWidth)];
    NSArray *originXArray2 = @[@(0),@(0.03*printerByteWidth),@(0.27*printerByteWidth),@(0.45*printerByteWidth)];
    NSString *partingLine = [NSString getPrintPartingLineWithByteWidth:printerByteWidth originX:0];
    
    // 分割线
    NSData *data = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data];
    
    // 标题栏
    NSString *titleStr = [NSString getPrintTextWithTextInfoArray:@[
                          @{@"商品":originXArray[0]},
                          @{@"数量":originXArray[1]},
                          @{@"单价":originXArray[2]},
                          @{@"金额":originXArray[3]},
    ] font:(NSStringFontSmall)];
    NSData *data2 = [self.escPrinter printSamallText:titleStr font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data2];
    
    // 分割线
    NSData *data3 = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
    [printStrArray addObject:data3];
    
    // 商品sku
    NSArray <SAProductListModel *>*procutArray = [productSkuArray filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id  _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
        SAProductListModel *model = evaluatedObject;
        return model.productCode.length > 0;
    }]];
    
    for (SAProductListModel *product in procutArray) {
        NSString *title = [NSString stringWithFormat:@"%@,%@",product.productCode,product.productName];
        NSData *data4 = [self.escPrinter printText:title font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
        [printStrArray addObject:data4];
        for (SASaleSkuListModel *sku in product.skuList) {
            NSString *colourSize = [NSString stringWithFormat:@"%@/%@          ",sku.colourName,sku.sizeName];
            NSString *newColourSize = [colourSize getSubStringWithByteWidthMax:10 font:NSStringFontSmall];
            
            NSString *count = [self getTruncation:sku.count And:6];
            NSString *price = [self getTruncation:[NSString stringWithFormat:@"%0.2f",[sku.price doubleValue]/100.f] And:7];
            NSString *discountPrice = [self getTruncation:[NSString stringWithFormat:@" %0.2f",[sku.discountPrice doubleValue]/100.f] And:10];
            
            NSString *str = [NSString getPrintTextWithTextInfoArray:@[
                                  @{@"":originXArray2[0]},
                                  @{count:originXArray2[1]},
                                  @{price:originXArray2[2]},
                                  @{discountPrice:originXArray2[3]},
            ] font:(NSStringFontSmall)];
            title = @"";
            NSData *data5 = [self.escPrinter noLineprintText:newColourSize font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
            [printStrArray addObject:data5];
            
            NSData *data6 = [self.escPrinter printSamallText:str font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
            [printStrArray addObject:data6];
        }
        // 分割线
        NSData *data7 = [self.escPrinter printSamallText:partingLine font:(PrinterPrintTextFontSmall) alignment:(NSTextAlignmentLeft)];
        [printStrArray addObject:data7];
    }
    return printStrArray;
}



-(NSString *)getTruncation:(NSString *)str And:(NSInteger) num {
    if (str.length > num) {
        NSString *newStr = [str substringToIndex:num];
        return newStr;
    }else {
        return str;
    }
}

- (ESCPrinter *)escPrinter {
    if (!_escPrinter) {
         _escPrinter = [[ESCPrinter alloc] init];
    }
    return _escPrinter;
}

@end
